package com.pro.xwt.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.service.BEmployeeService;

@Controller
@RequestMapping("/manager")
public class AdminController {
	 
	@Autowired
	BEmployeeService bEmployeeService;
	

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String to(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

			return "manager/login";
		
	}
	/**
	 * 返回指示板index
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String back(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			return "manager/index";
		
	}
	/**
	 * 登出
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String out(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		session.setAttribute("admin", null);
			return "redirect:login";
		
	}
	/**
	 * 登录
	 */
	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String toSg(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("name") String name,@RequestParam("password") String password)
			throws Exception {
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("username", name);
		parameter.put("password", password);
		parameter.put("role", "ADMIN");
		parameter.put("deleteState", "0");
		List<BEmployee> tag= bEmployeeService.queryListAll(parameter);
		//判断
		if(tag !=null && tag.size()>=1){
			session.setAttribute("admin", tag.get(0));
			return "redirect:index";
		}else{
			model.addAttribute("errorTag", "error");
			return "manager/login";
		}
		
	}

}
